package com.lhwl.it.sb.action;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.lhwl.it.sb.service.SbService;
import com.lhwl.it.sbfl.service.SbflService;
import com.lhwl.it.sbfl.service.SbfltysxService;
import com.lhwl.it.util.PageBean;
import com.lhwl.it.util.RequestMap;

@Controller
@RequestMapping("/itmanager/sb")
public class SbAction {

	private Log log = LogFactory.getLog(this.getClass());
	@Resource
	private SbService sbService;
	@Resource
	private SbflService sbflService;
	@Resource
	private SbfltysxService sbfltysxService;

	@RequestMapping()
	public String index(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HashMap map = new RequestMap(request);
		List sbfl = this.sbflService.list("SbflMapper.listAll", map);
		request.setAttribute("sbfl", sbfl);
		return "/WEB-INF/jsp/sb/sbgl";
	}

	@RequestMapping(value = "/json", method = RequestMethod.POST)
	public String sbJson(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		HashMap map = new RequestMap(request);
		HashMap mp=new HashMap();
		PageBean pageBean = this.sbService.getPage("SbMapper.list", map);
		List list = pageBean.getList();
		StringBuffer sbuff=new StringBuffer("");
		for (int i = 0; i < list.size(); i++) {
			HashMap lm = (HashMap) list.get(i);
			if(lm.get("sbsx")!=null){
				String sbsx = lm.get("sbsx").toString();
				String[] sb = sbsx.split(",");
				for (int j = 0; j < sb.length; j++) {
					String csb = sb[j];
					String[] ssb = csb.split("=");
					mp=this.sbfltysxService.get("SbfltysxMapper.getSxmc",ssb[0]);
					ssb[0]=mp.get("tysxmc").toString();
					sbuff.append("["+ssb[0]+":<font color='blue'>"+ssb[1]+"</font>]");
				}
				lm.remove("sbsx");
				lm.put("sbsx", sbuff.toString());
				sbuff.setLength(0);
			}
		}
		
		request.setAttribute("smap", map);
		JSONArray jsonArray = JSONArray.fromObject(list);
		JSONObject json = new JSONObject();
		json.put("total", pageBean.getRowscount());
		json.put("rows", jsonArray);
		response.getWriter().print(json);
		return null;
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String add(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HashMap map = new RequestMap(request);
		try {
			this.sbService.insert("SbMapper.insert", map);
			response.getWriter().print("success");
		} catch (Exception e) {
			e.printStackTrace();
			response.getWriter().print("error");
		}
		return null;
	}

	@RequestMapping(value = "/editZt", method = RequestMethod.POST)
	public String editZt(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HashMap map = new RequestMap(request);
		try {
			this.sbService.update("SbMapper.editZt", map);
			response.getWriter().print("success");
		} catch (Exception e) {
			e.printStackTrace();
			response.getWriter().print("error");
		}
		return null;
	}
	
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	public String edit(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HashMap map = new RequestMap(request);
		try {
			this.sbService.update("SbMapper.edit", map);
			response.getWriter().print("success");
		} catch (Exception e) {
			e.printStackTrace();
			response.getWriter().print("error");
		}
		return null;
	}
	
	@RequestMapping(value = "/sbfllist",method = RequestMethod.POST)
	public String sbflList(HttpServletRequest request, HttpServletResponse response)throws Exception {
		HashMap map = new RequestMap(request);
		List list=this.sbService.list("SbMapper.sbfllist", map);
		JSONArray json=JSONArray.fromObject(list);
		response.getWriter().print(json);
		return null;
	}
	
	
	@RequestMapping(value = "/show")
	public String show(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HashMap map = new RequestMap(request);
		HashMap mp=new HashMap();
		String id = map.get("id").toString();
		map.put("id", id);
		HashMap m = this.sbService.getMap("SbMapper.getSb", map);
		String sbsx=m.get("sbsx").toString();
		StringBuffer sbuff=new StringBuffer("");
		String[] sb = sbsx.split(",");
		for (int j = 0; j < sb.length; j++) {
			String csb = sb[j];
			String[] ssb = csb.split("=");
			mp=this.sbfltysxService.get("SbfltysxMapper.getSxmc",ssb[0]);
			ssb[0]=mp.get("tysxmc").toString();
			sbuff.append("["+ssb[0]+":<font color='blue'>"+ssb[1]+"</font>]");
		}
		m.remove("sbsx");
		m.put("sbsx", sbuff.toString());
		JSONObject json = JSONObject.fromObject(m);
		response.getWriter().print(json);
		return null;
	}
	
	@RequestMapping(value = "/toedit")
	public String toedit(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HashMap map = new RequestMap(request);
		HashMap mp=new HashMap();
		HashMap ms=new HashMap();
		String id = map.get("id").toString();
		map.put("id", id);
		HashMap m = this.sbService.getMap("SbMapper.getSb", map);
		String sbsx=m.get("sbsx").toString();
		String[] sb = sbsx.split(",");
		for (int j = 0; j < sb.length; j++) {
			String csb = sb[j];
			String[] ssb = csb.split("=");
			ms.put(ssb[0], ssb[1]);
		}
		m.remove("sbsx");
		m.put("sbsx", ms);
		
		JSONObject json = JSONObject.fromObject(m);
		response.getWriter().print(json);
		return null;
	}
	

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public String deleteYg(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		HashMap map = new RequestMap(request);
		String id = map.get("ids").toString();
		String[] ids = id.split(",");
		try {
			for (int i = 0; i < ids.length; i++) {
				map.remove("id");
				map.put("id", ids[i]);
				this.sbService.delete("SbMapper.delete", map);
			}
			response.getWriter().print("success");
		} catch (Exception e) {
			e.printStackTrace();
			response.getWriter().print("error");
		}
		return null;
	}

}
